<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/5/28
 * Time: 11:25
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Banner extends Base {
    /**
     * 列表展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $map = array();
            extract(input());
            if(isset($keyword)&&$keyword!="")
            {
                $map['banner_title'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($province)&&$province!="")
            {
                $map['banner_status'] = $province;
            }
            if(isset($start) && $start != '' && isset($end) && $end != ''){
                $map['banner_time'] = ['between', [$start, $end]];
            }
            if(isset($start) && $start != '' && isset($end) && $end == ''){
                $map['banner_time'] = ['> time', $start];
            }
            if(isset($start) && $start == '' && isset($end) && $end != ''){
                $map['banner_time'] = ['< time', $end];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('banner')
                ->where($map)->count();// 获取总条数
            $lists = Db::name('banner')
                ->where($map)
                ->page($NowPage,$limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * 修改排序
     * @return \think\response\Json
     */
    public function sort(){
        $param = input('');
        $res = Db::table('up_banner')->where('banner_id', $param['id'])->setField('banner_sort', $param['value']);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' => '修改失败']);
        }
    }

    /***
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $task_id = input('param.id');
        $res = Db::name('banner')->where('banner_id',$task_id)->delete();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }

    /***
     * @return \think\response\Json
     * 上架
     */
    public function up(){
        $task_id = input('param.id');
        $res = Db::name('banner')->where('banner_id',$task_id)->setField('banner_status',1);
        if($res){
            return json(['code'=>200,'msg'=>'上架成功']);
        }else{
            return json(['code'=>100,'msg'=>'上架失败']);
        }
    }

    /***
     * @return \think\response\Json
     * 下架
     */
    public function down(){
        $task_id = input('param.id');
        $res = Db::name('banner')->where('banner_id',$task_id)->setField('banner_status',2);
        if($res){
            return json(['code'=>200,'msg'=>'下架成功']);
        }else{
            return json(['code'=>100,'msg'=>'下架失败']);
        }
    }

    /***
     * @return mixed|\think\response\Json
     * 添加
     */
    public function add(){
        if(Request::instance()->isPost()){
            $banner_title =input('banner_title');
            $banner_sort= input('banner_sort');
            $banner_thumb = input('banner_thumb');
            $banner_type = input('banner_type');
            $banner_link = input('banner_link');
            $wy_notice= input('wy_notice');
            $topic_notice= input('topic_notice');

            if($banner_type == 1){
                if(empty($banner_link)){
                    return json(['code' => 100, 'msg' => '请填写外部链接']);
                }
            }elseif($banner_type == 2){
                if(empty($wy_notice)){
                    return json(['code' => 100, 'msg' => '物业通知不能为空']);
                }
            }elseif($banner_type == 3){
                if(empty($topic_notice)){
                    return json(['code' => 100, 'msg' => '物业通知不能为空']);
                }
            }

            $data['banner_title'] = $banner_title;
            $data['banner_thumb'] = $banner_thumb;
            $data['banner_type'] = $banner_type;
            $data['banner_sort'] = $banner_sort;
            $data['banner_time'] = date('Y-m-d H:i:s',time());
            if($banner_type == 1){
                $data['banner_link'] = $banner_link;
            }
            if($banner_type == 2){
                $data['banner_object_id'] = $wy_notice;
            }
            if($banner_type == 3){
                $data['banner_object_id'] = $topic_notice;
            }
            $res = Db::table('up_banner')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $wy_notice = Db::table('up_wy_notice')->order('wy_time desc')->select();
        $topic_notice = Db::table('up_topic')->where('topic_is_show',1)->order('topic_fb_time desc')->select();
        $this->assign('wy_notice', $wy_notice);
        $this->assign('topic_notice', $topic_notice);
        return $this->fetch();
    }

    /***
     * @return mixed|\think\response\Json
     * 编辑
     */
    public function update(){
        if(Request::instance()->isPost()){
            $id = input('id');
            $banner_title =input('banner_title');
            $banner_sort= input('banner_sort');
            $banner_thumb = input('banner_thumb');
            $banner_type = input('banner_type');
            $banner_link = input('banner_link');
            $wy_notice= input('wy_notice');
            $topic_notice= input('topic_notice');

            if($banner_type == 1){
                if(empty($banner_link)){
                    return json(['code' => 100, 'msg' => '请填写外部链接']);
                }
            }elseif($banner_type == 2){
                if(empty($wy_notice)){
                    return json(['code' => 100, 'msg' => '物业通知不能为空']);
                }
            }elseif($banner_type == 3){
                if(empty($topic_notice)){
                    return json(['code' => 100, 'msg' => '物业通知不能为空']);
                }
            }

            $data['banner_title'] = $banner_title;
            $data['banner_type'] = $banner_type;
            $data['banner_sort'] = $banner_sort;
            $data['banner_time'] = date('Y-m-d H:i:s',time());
            if($banner_type == 1){
                $data['banner_link'] = $banner_link;
                $data['banner_object_id'] = '';
            }
            if($banner_type == 2){
                $data['banner_object_id'] = $wy_notice;
                $data['banner_link'] = '';
            }
            if($banner_type == 3){
                $data['banner_object_id'] = $topic_notice;
                $data['banner_link'] = '';
            }
            if(!empty($banner_thumb)){
                $data['banner_thumb'] = $banner_thumb;
            }

            $res = Db::table('up_banner')->where('banner_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $id = input('banner_id');
        $info= Db::table('up_banner')->where('banner_id',$id)->find();
        //物业通知
        $wy_notice = Db::table('up_wy_notice')->order('wy_time desc')->select();
        //话题
        $topic_notice = Db::table('up_topic')->where('topic_is_show',1)->order('topic_fb_time desc')->select();
        $this->assign('wy_notice', $wy_notice);
        $this->assign('topic_notice', $topic_notice);
        $this->assign('id',$id);
        $this->assign('res',$info);
        return $this->fetch();
    }
}